//Window kjører ved oppstart
//http://api.jquery.com/load-event/

var watchId;
var options = {enableHighAccuracy:true, timeout:3000};



function errorCallback(error) 
{
   var errorInfo = document.getElementById("locationInfo");

   switch (error.code) 
   {
      case error.PERMISSION_DENIED:         
         errorInfo.innerHTML = "User denied the request for Geolocation.";
         break;
      case error.POSITION_UNAVAILABLE:
         errorInfo.innerHTML = "Location information is unavailable.";
         break;
      case error.TIMEOUT:
         errorInfo.innerHTML = "The request to get user location timed out.";
         break;
      case error.UNKNOWN_ERROR:
         errorInfo.innerHTML = "An unknown error occurred.";
         break;
   }
}

//Me legger til min funsjonalitetet in array :) javascript ftw


function debugLocation() 
{
	//Ved
	var debBugLat = 60.388176; 
	var deBugLang = 5.325043;
	
	showClosestSong(debBugLat, deBugLang);
	 
}

function successCallback(position) 
{
	
    if (position.coords.latitude === 0 && position.coords.longitude === 0) {
    	document.getElementById("locationInfo").innerHTML = ('Unable to acquire your location');
		}
		
		document.getElementById("locationInfo").innerHTML = "Latitude: " +   
		position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
		showClosestSong(position.coords.latitude, position.coords.longitude);
}

function showClosestSong(lat, lang){
		//Distance frå dette til eit anna
	$.getJSON( "data/songs.json", function( json ) {
		//Legg til distanse
	  	$.each(json.playlist.songs, function( key, value ) {
	  		console.log( key + ": " + value.location );
	  		var distance = getDistanceFromLatLonInM(lat, lang, value.lat, value.lang); 		
	  		
	  		json.playlist.songs[key].distance = distance;
	//  		console.log("Dist: "+json.playlist.songs[key].distance);
	  	});
	 
	 //Sorter listen etter distanse (kanskje betre å finne min, mennø latskap forhindrer klokskap kl 17.
	 json.playlist.songs.sort(function(a,b) {
	 	return a.distance - b.distance;
	 });
	 
	 document.getElementById("closesong").innerHTML = "Distance: " + json.playlist.songs[0].distance.toFixed(2) + " Tit: " + json.playlist.songs[0].title + " Tit: " + json.playlist.songs[0].location; 

	 
	 });
}

//La oss 

function watchFunc() 
{
   if (navigator.geolocation) 
   {
      watchId = navigator.geolocation.watchPosition(successCallback, errorCallback);
   } 
   else 
   {
      document.getElementById("locationInfo").innerHTML = "Geolocation is not supported.";
   }
}

function stopWatchFunc() 
{
   if (navigator.geolocation) 
   {
      navigator.geolocation.clearWatch(watchId);
   } 
   else 
   {
      document.getElementById("locationInfo").innerHTML = "Geolocation is not supported.";
   }
}  

function getDistanceFromLatLonInM(lat1,lon1,lat2,lon2) {
  var R = 6371000; // Radius of the earth in km 
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}